package boundary;
import control.*;
import entity.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.ImageIcon;


public class InventoryPanel extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7L;
	private static boolean isChinese;
    private Timer timer;
	private Dice dice;
	private ItemController itemControl=new ItemController();
	Image img;
	int _x=0, _y=0;
	ArrayList<GameMovableObj> allPecies = GameMovableObj.getGameMovableObj();
	ArrayList<GameClickableObj> allClickObj = GameClickableObj.getGameClickableObj();
	
	int count = 0;
    public InventoryPanel() {
		this(new ImageIcon("mono.PNG").getImage());
        setFocusable(true);
        setBackground(Color.WHITE);
        setDoubleBuffered(true);
		dice = new Dice("/images/dice/Dices.PNG",47,57);
		itemControl.storeInventory(this);
        timer = new Timer(25, this);
    }

	public InventoryPanel(Image img) {
		this.img = img;
		Dimension size = new Dimension(400, 300);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
		}

	public void paintComponent(Graphics g) {
		g.setColor(Color.BLACK);
		g.fillRect(0,0,800,800);

		dice.setNextImage();
	}


    public void paint(Graphics g) {
    	 super.paint(g);
         Graphics2D g2d = (Graphics2D)g;
         int rectX = this.getWidth() - dice.getX()-47;
         int Width = rectX/allPecies.size();
         int tempWidth = Width;
         int tempX = dice.getX()+48;
         for(int i=0; i<allPecies.size();i++){
         	g2d.setColor(Color.WHITE);
 			g2d.fillRect(tempX,0, tempWidth, this.getHeight());
 			g2d.setColor(Color.RED);
 			g2d.drawLine(tempX, 0, tempX, this.getHeight());
 			Pecies p = (Pecies)allPecies.get(i);
 			g2d.drawImage(p.getImageIcon(), tempX+5, 10, 50, 50, null);
 			g2d.drawString(p.getName(),tempX+55 ,20);
 			if(isChinese){
 				g2d.drawString("self-chi",tempX+5, 75);
 				if(p.isItemUsing())
 					g2d.drawString("item-chi", tempX+5, 90);
 			}
 			else{
 				g2d.drawString("Self-esteem:",tempX+5, 75);
 				if(p.isItemUsing())
 					g2d.drawString("Item use:", tempX+5, 90);
 			}
 			itemControl.drawInventoryPanel(g2d, p, tempX+5, 95);
 			g2d.drawString(Integer.toString(p.getSelf()), tempX+75, 75);
 			if(p.isItemUsing())
 				g2d.drawString(p.getItemUsing().getItemName(), tempX+55, 90);
 			tempX += Width;
         }
         g2d.drawImage(dice.getImage(),dice.getX(),dice.getY(),this);
         Toolkit.getDefaultToolkit().sync();
         g.dispose();
    }

    public void actionPerformed(ActionEvent e) {
        repaint();  
    }
    public void doRepaint() {
    	repaint();
    }
	public void startRadomDice() {
		timer.start();
	}
	public int stopRadomDice() {
		timer.stop();
		return dice.getValue();
	}

	public Timer getTimer() {
		return timer;
	}

	public static void setLanguage(boolean isChi){
		isChinese = isChi;
	}
}
